// https://h3manth.com/new/blog/2013/negative-array-index-in-javascript/

// intrinsic
// attend



function deepCopyExu(childCopy, o, n) {
    if (o instanceof Array) {
        n = n || [];
        for (var i = 0; i < o.length; ++i) {
            n[i] = childCopy(o[i]);
        }
        return n;

    } else if (o instanceof Object) {
        n = n || {};
        for (var i in o) {
            n[i] = childCopy(o[i]);
        }
        return n;
    } else {
        return o;
    }
};

function deepCopy() {
    var args = [].slice.call(arguments);
    args.unshift(deepCopy);
    return deepCopyExu.apply(null, args);
};

var items = [];
function addItem(item) {
    console.log(JSON.stringify(item)) 


    var json = {"StackLmt":1000}
    //  jsonItem[item.typeId];
    if (null == json) {
        return;
    }

    var copy = deepCopy(item);
    // var items = this._items;
    var i = 0;
    for (; i < items.length; ++i) {
        var item1 = items[i];
        //根据配置表中的id排序
        if (item1.typeId > item.typeId) {
            break;
        }
    }
    var item2 = items[i-1];
    //按照物品id排序，该物品已存在
    if (item2 != null && item2.typeId == copy.typeId) {
        item2.count += copy.count;
        //超过玩家道具唯一上限，设置为上限值
        if(item2.count>json.StackLmt){
            item2.count = json.StackLmt;
        }
    }else{
        if(copy.count>json.StackLmt){
            copy.count=json.StackLmt;
        }
        //上限设置可能为0，需要修改配置表
        console.log('item count:'+copy.count);
        //该物品不存在，在此插入该物品包裹
        //todo 配置表有问题可能唯一上限为0
        if(copy.count>0){
            items.splice(i, 0, copy);
            // items.push(copy)
        }
    }
}

addItem({typeId: 3, count: 1});

addItem({typeId: 1, count: 1});

addItem({typeId: 1, count: 1});



// var _array = []

// _array.push({"name":"haha","count":1})
// var _array_item = _array[0]
// _array_item.count = 2
// console.log("finished")

// message Item {
//     required uint32 typeId = 1;
//     required uint32 count = 2;
// }


// <<<<<<< HEAD
var _array  = [1,2]
console.log(_array);
// =======
var mTest = [1,2,3]
var result = mTest.splice(0,1)
// mTest [ 2, 3 ]
// result [1]

var emptyArray = [1]
// emptyArray.push(1)
if(emptyArray) 
    console.log("emptyArray", emptyArray); //this
else
    console.log("!emptyArray");
// >>>>>>> 8987307907162de68be627ab8a4cb1b8a94c07c7

